import pymysql as ps
from pyecharts import options
from pyecharts.charts import Pie

db = ps.connect(
    host="localhost",
    port=3306,
    user="root",
    database="crawlerdemo",
    password="458092",
    charset="utf8"
)
cursor = db.cursor()

sql = "select count(*) from job"

try:
    cursor.execute(sql)
    res = cursor.fetchone()
    if res is not None:
        total = int(res[0])
except:
    print("查询出错了")

sql = ("select job_place,Round(count(job_id)/%s,3) from job group by job_place order by count(job_id) desc")
try:
    cursor.execute(sql,(total))
    res = cursor.fetchall()

    pie = Pie()
    pie.add("百分比",res)

    pie.set_global_opts(title_opts=options.TitleOpts(title="城市岗位占比图"))
    pie.render("pieportion.html")
except:
    print("查询出错了")

cursor.close()
db.close()